package com.tian.service.impl;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tian.common.CommonResult;
import com.tian.common.PageResult;
import com.tian.dto.ChargeActivityConditionAddReqDto;
import com.tian.dto.ChargeActivityConditionPageReqDto;
import com.tian.dto.ChargeActivityConditionRespDto;
import com.tian.dto.ChargeActivityConditionUpdateReqDto;
import com.tian.entity.ChargeActivityCondition;
import com.tian.enums.ResultCode;
import com.tian.mapper.ChargeActivityConditionMapper;
import com.tian.service.ChargeActivityConditionService;
import com.tian.util.ChargeMarketRedisKeyPrefix;
import com.tian.util.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RBucket;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * {@code @description:} 活动条件
 *
 * @author tianwc 公众号：Java后端技术全栈
 * 在线刷题 1200+java面试题和1000+篇技术文章：<a href="https://woaijava.cc/">博客地址</a>
 * {@code @date:} 2024-08-05 22:47
 * {@code @version:} 1.0
 */
@Slf4j
@Service
public class ChargeActivityConditionServiceImpl implements ChargeActivityConditionService {

    @Resource
    private ChargeActivityConditionMapper chargeActivityConditionMapper;
    @Resource
    private RedissonClient redissonClient;

    @Override
    public CommonResult<Boolean>  add(ChargeActivityConditionAddReqDto chargeActivityConditionAddReqDto) {
        ChargeActivityCondition chargeActivityCondition = new ChargeActivityCondition();
        BeanUtils.copyProperties(chargeActivityConditionAddReqDto, chargeActivityCondition);
        chargeActivityConditionMapper.insert(chargeActivityCondition);
        redissonClient.getBucket(ChargeMarketRedisKeyPrefix.ACTIVITY_CONDITION_KEY + chargeActivityCondition.getId()).set(JSON.toJSONString(chargeActivityCondition));
        return CommonResult.success(Boolean.TRUE);
    }

    @Override
    public CommonResult<PageResult<List<ChargeActivityConditionRespDto>>> page(ChargeActivityConditionPageReqDto chargeActivityConditionPageReqDto) {
        Page<Object> page = PageHelper.startPage(chargeActivityConditionPageReqDto.getCurrentPage(), chargeActivityConditionPageReqDto.getPageSize());

        List<ChargeActivityCondition> chargeActivityConditions = chargeActivityConditionMapper.page(chargeActivityConditionPageReqDto);

        List<ChargeActivityConditionRespDto> chargeActivityConditionRespDtoList = new ArrayList<>();

        chargeActivityConditions.forEach(chargeActivityCondition -> {
            ChargeActivityConditionRespDto chargeActivityConditionRespDto = new ChargeActivityConditionRespDto();
            BeanUtils.copyProperties(chargeActivityCondition, chargeActivityConditionRespDto);
            chargeActivityConditionRespDtoList.add(chargeActivityConditionRespDto);
        });

        PageResult<List<ChargeActivityConditionRespDto>> listPageResult = PageResult.setData((int) page.getTotal(), page.getPageSize(), page.getPages(), page.getPageNum(), chargeActivityConditionRespDtoList);
        return CommonResult.success(listPageResult);
    }

    @Override
    public CommonResult<Boolean>  update(ChargeActivityConditionUpdateReqDto chargeActivityConditionUpdateReqDto) {
        ChargeActivityCondition chargeActivityCondition = new ChargeActivityCondition();
        BeanUtils.copyProperties(chargeActivityConditionUpdateReqDto, chargeActivityCondition);
        chargeActivityConditionMapper.updateByPrimaryKey(chargeActivityCondition);
        redissonClient.getBucket(ChargeMarketRedisKeyPrefix.ACTIVITY_CONDITION_KEY + chargeActivityCondition.getId()).set(JSON.toJSONString(chargeActivityCondition));
        return CommonResult.success(Boolean.TRUE);
    }

    @Override
    public CommonResult<Boolean>  delete(ChargeActivityConditionUpdateReqDto chargeActivityConditionUpdateReqDto) {
        chargeActivityConditionMapper.deleteByPrimaryKey(chargeActivityConditionUpdateReqDto.getId());
        redissonClient.getBucket(ChargeMarketRedisKeyPrefix.ACTIVITY_CONDITION_KEY + chargeActivityConditionUpdateReqDto.getId()).delete();
        return CommonResult.success(Boolean.TRUE);
    }

    @Override
    public CommonResult<ChargeActivityConditionRespDto> findById(Integer id) {

        RBucket<String> bucket = redissonClient.getBucket(ChargeMarketRedisKeyPrefix.ACTIVITY_CONDITION_KEY + id);
        String cache = bucket.get();
        if (StringUtil.isBlank(cache)) {
            ChargeActivityCondition chargeActivityCondition = chargeActivityConditionMapper.selectByPrimaryKey(id);
            if (chargeActivityCondition == null) {
                log.error("活动条件id不存在,id={}", id);
                return CommonResult.failed(ResultCode.PARAMETER_ERROR);
            }
            redissonClient.getBucket(ChargeMarketRedisKeyPrefix.ACTIVITY_CONDITION_KEY + chargeActivityCondition.getId()).set(JSON.toJSONString(chargeActivityCondition));
            ChargeActivityConditionRespDto chargeActivityConditionRespDto = new ChargeActivityConditionRespDto();
            BeanUtils.copyProperties(chargeActivityCondition, chargeActivityConditionRespDto);
            return CommonResult.success(chargeActivityConditionRespDto);
        }
        ChargeActivityCondition chargeActivityCondition = JSON.parseObject(cache, ChargeActivityCondition.class);
        ChargeActivityConditionRespDto chargeActivityConditionRespDto = new ChargeActivityConditionRespDto();
        BeanUtils.copyProperties(chargeActivityCondition, chargeActivityConditionRespDto);
        return CommonResult.success(chargeActivityConditionRespDto);
    }
}
